/* 
 * File:   Derivada_DifDivididas.cpp
 * Author: ginancm
 * 
 * Created on 12 de enero de 2012, 06:00 PM
 */

#include "Derivada_DifDivididas.h"
#include <cstdio>
#include <iostream>

using namespace std;

Derivada_DifDivididas::Derivada_DifDivididas(long double Xs[], int n)
{
    nDatos=n;

    array_datosX= new long double [nDatos];
   
    for(int i=0;i<nDatos;i++)
    {
        array_datosX[i]= Xs[i];
    }
    
    
}

Derivada_DifDivididas::~Derivada_DifDivididas() {
    
    if(array_datosX!=NULL)
    {
        delete array_datosX;
        array_datosX=NULL;
    }
}

long double Derivada_DifDivididas::funcion(long double x)
{
    long double p;
    long double t=(long double)x;
    p = 165.0*exp(0.02*t);
    
    return p;
}

long double Derivada_DifDivididas::derivadaTeorica(long double x)
{
    long double d;
    
    d=3.3*exp(0.02*x);
    return d;
}

long double Derivada_DifDivididas::derivadaTeorica2(long double x)
{
    long double d;
    
    d = 0.066*exp(0.02*x);
    return d;
}

long double Derivada_DifDivididas::derivadaTeorica3(long double x)
{
    long double d;
    
    d = 0.00132*exp(0.02*x);
    return d;
}

long double Derivada_DifDivididas::CalcularAdelante(long double x, long double x1, long double hi)
{
    long double Xi = x;
    long double Xi_1=x1; // Variable Xi+1
    long double h = hi; //Distancia h dada
    long double derivada;
       
    derivada =  (funcion(Xi_1)-funcion(Xi))/h;
    return derivada;
    
}

long double Derivada_DifDivididas::ErrorAproxAdelAtras(long double x1, long double x2, long double hi)
{
    long double h = hi;
    long double derivada=maximoDeriv2(x1, x2);
    long double error;
    
    error=(pow(h, 2.0)/2.0)*derivada;
    return error;
    
}

long double Derivada_DifDivididas::maximoDeriv2(long double x1, long double x2)
{
    long double y1, y2;
  
    y1=abs(derivadaTeorica2(x1));
    y2=abs(derivadaTeorica2(x2));
    
    if(y1>y2) return y1;
    else return y2;             
}


long double Derivada_DifDivididas::CalcularAtras(long double x1, long double x, long double hi)
{
    long double X1_i = x1; //Variable Xi-1
    long double Xi = x; 
    long double h = hi; //Distancia h dada
    long double derivada;
    
    derivada = (funcion(Xi)-funcion(X1_i))/h;
    return derivada;
}

long double Derivada_DifDivididas::CalcularCentradas(long double x1, long double x2, long double hi)
{
    
    long double Xi_1 = x1; //Variable Xi+1
    long double X1_i = x2; //Variable Xi-1
    long double h = hi; //Distancia h dada
    long double derivada;
    
    derivada = (funcion(Xi_1)-funcion(X1_i))/2*h;
    return derivada;
}

long double Derivada_DifDivididas::maximoDeriv3(long double x1, long double x2, long double x3)
{
    long double y0, y1, y2;
    
    y0=abs(derivadaTeorica3(x1));
    y1=abs(derivadaTeorica3(x2));
    y2=abs(derivadaTeorica3(x3));
    
    if (y0>y1)
    {
        if(y0>y2) return y0;
        else return y2;
    }
    else
    {
        if (y1>y2) return y1;
        else return y2;
    }
}

long double Derivada_DifDivididas::ErrorAproxCentradas(long double x1, long double x, long double x2, long double hi)
{
    long double deriv, error;
   
    deriv = maximoDeriv3(x, x1, x2);
    error = (pow(hi,2.0)/6.0)*deriv;
    return error;
}

